import 'dart:io';

import 'package:commons/commons.dart' hide Storage;
import 'package:qiniu_flutter_sdk/qiniu_flutter_sdk.dart';

class QiniuMan extends GetxController {
  final Storage storage = Storage();
  final put = PutController();

  Future<void> upload(String path) async {
    final file = File(path);
    final key = file.path.split('/').last;
    final result = await storage.putFile(file, 'token',
        options: PutOptions(
          key: key,
        ));
    Get.log('result: $result');
  }

  void cancel() {
    put.cancel();
  }

  void _listener(double progress) {
    Get.log('progress: $progress');
  }

  void _onStatus(StorageStatus status) {
    Get.log('status: $status');
  }

  @override
  void onInit() {
    super.onInit();
    put.addProgressListener(_listener);
    put.addStatusListener(_onStatus);
  }

  @override
  void onClose() {
    put.removeProgressListener(_listener);
    put.removeStatusListener(_onStatus);
    put.cancel();

    super.onClose();
  }
}

extension QiniuEx on GetInterface {
  QiniuMan get qiniu => Get.find<QiniuMan>();
}
